/*
 * CLogon.cpp
 *
 *      Author: grifos
 */

#include "CLogon.h"

CLogon::CLogon(float x, float y, float z , char* texPath, int bliting, float blittime) {
	_display = 1;
	_blitTime = blittime;
	_bliting = bliting;
	_timeElapsed = 0;
	mX = x;
	mY = y;
	mZ = z;
	loadTexture(texPath);
	mBaseNode = new osg::PositionAttitudeTransform(); // noeud permettant de définir l'endroit ou se trouve le cube
	mCubeRotation = new osg::MatrixTransform(); // noeud permettant de faire tourner le cube
	mCubeGeode = new osg::Geode(); // noeud contenant les primitives

	mCubeGeode->setDataVariance(osg::Object::DYNAMIC);

	mBaseNode->addChild(mCubeRotation.get());
	mCubeRotation->addChild(mCubeGeode.get());
	// On crée le quad qui prendra la texture
	osg::ref_ptr<osg::Geometry> textGeometry = new osg::Geometry;
	mCubeGeode->addDrawable(textGeometry.get());


	osg::ref_ptr<osg::Vec3Array> textVerts = new osg::Vec3Array(4);
	(*textVerts)[0] = osg::Vec3(5.916666667f, 1, 0);
	(*textVerts)[1] = osg::Vec3( 0, 1, 0);
	(*textVerts)[2] = osg::Vec3( 0, 0, 0);
	(*textVerts)[3] = osg::Vec3(5.916666667f, 0, 0);

	textGeometry->setVertexArray(textVerts.get());

	osg::ref_ptr<osg::Vec2Array> textTexCoords = new osg::Vec2Array(4);
	(*textTexCoords)[0].set(1.0f,0.0f);
	(*textTexCoords)[1].set(0.0f,0.0f);
	(*textTexCoords)[2].set(0.0f,1.0f);
	(*textTexCoords)[3].set(1.0f,1.0f);
	textGeometry->setTexCoordArray(0,textTexCoords.get());

	textGeometry->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::QUADS,0,4));

	// Need to assign a color to the underlying geometry, otherwise we'll get
	// whatever color is current applied to our geometry.
	// Create a color array, add a single color to use for all the vertices

	osg::ref_ptr<osg::Vec4Array> colorArray = new osg::Vec4Array;
	colorArray->push_back(osg::Vec4(1.0f, 1.0f, 1.0f, 1.0f) ); // white, fully opaque

	// An index array for assigning vertices to colors (based on index in the array)
	osg::TemplateIndexArray
	  <unsigned int, osg::Array::UIntArrayType,4,1> *colorIndexArray;
	colorIndexArray =
	  new osg::TemplateIndexArray<unsigned int, osg::Array::UIntArrayType,4,1>;
	colorIndexArray->push_back(0);

	// Use the index array to associate the first entry in our index array with all
	// of the vertices.
	textGeometry->setColorArray( colorArray.get());

	textGeometry->setStateSet(mTextureState);

	osg::Vec3 position(mX,mY,mZ);
	// On place le texte où on veut
	mBaseNode->setPosition(position);
	mCubeRotation->preMult( osg::Matrix::rotate( gmtl::Math::deg2Rad( 180.0f ), 1.0f, 0.0f, 0.0f) );

}

CLogon::~CLogon() {
}

void CLogon::update(float t)
{
	if(_bliting==1)
	{
		_timeElapsed += t;
		if(_timeElapsed>= _blitTime)
		{
			_timeElapsed = 0;
			if(_display==1)
			{
				_display=0;
				mBaseNode->removeChild(mCubeRotation.get());
			}
			else
			{
				_display=1;
				mBaseNode->addChild(mCubeRotation.get());
			}
		}
	}
}

void CLogon::loadTexture(char *path)
{
	mTexture = new osg::Texture2D;
	mTexture->setImage(osgDB::readImageFile(path));

	osg::AlphaFunc* alphaFunc = new osg::AlphaFunc;
	alphaFunc->setFunction(osg::AlphaFunc::GEQUAL,0.05f);

	mTextureState = new osg::StateSet;

	mTextureState->setMode( GL_LIGHTING, osg::StateAttribute::OFF );
	mTextureState->setTextureAttributeAndModes
		  (0, mTexture, osg::StateAttribute::ON );
	mTextureState->setAttributeAndModes
		  (new osg::BlendFunc, osg::StateAttribute::ON );
	osg::AlphaFunc* alphaFunction = new osg::AlphaFunc;
	alphaFunction->setFunction(osg::AlphaFunc::GEQUAL,0.05f);
	mTextureState->setAttributeAndModes( alphaFunc, osg::StateAttribute::ON );
}
